import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
import { getLocal } from '@/utils/localStore'
import { ElMessage } from 'element-plus'
import useStore from '@/store'
import { getMenu } from '@/utils/menuRoute'
import { close, start } from '@/utils/nporgress'
const routes = [
  {
    path: '/login',
    component: () => import('@/views/login.vue'),
    hidden: true,
  },
  {
    path: '/404',
    component: () => import('@/views/404.vue'),
    hidden: true,
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '检测大屏', icon: 'icon_10' },
      },
    ],
  },
]
const router = createRouter({
  history: createWebHistory(),
  routes,
})
const modules = import.meta.glob('../pages/*/*/*.vue')
const modulesLayout = import.meta.glob('../layout/*.vue')
router.beforeEach(async (to, from, next) => {
  start()
  // 定义标识，记录路由是否添加
  let token = getLocal('token')
  if (token) {
    // 登录之后通过用户得角色id来获取动态菜单
    const { user, roleRoute } = await useStore()
    const roleId = user.userRole || getLocal('userRole')
    const menuRoute = await getMenu(roleId) //通过用户得角色id来获取动态菜单
    let registerRouteFresh = roleRoute.registerRouteFresh
    // console.log(registerRouteFresh)
    if (registerRouteFresh) {
      //首次登录添加路由为true
      // const dynamicRoute = roleRoute.routeList || JSON.parse(getLocal("roleRoute"))
      // console.log(dynamicRoute)
      menuRoute.forEach((v: any) => {
        router.addRoute(v)
      })
      // console.log(menuRoute)
      // const treeData = construct(dynamicRoute, {
      //   id: "id",
      //   pid: "parent_id",
      //   children: "children"
      // })
      // let routes = []
      // for (let p of treeData) {
      //   if (p.type == 'menu') {
      //     console.log(p.route)
      //     const route = {
      //       path: p.route,
      //       component: modulesLayout[`../layout/index.vue`],
      //       name: p.route,
      //       meta: { title: p.name },
      //       children: []
      //     }
      //     if (p.children && p.children.length > 0) {
      //       for (let c of p.children) {
      //         const son_route = {
      //           path: c.route,
      //           component: modules[`../pages${c.route}.vue`],
      //           name: c.route,
      //           meta: { title: c.name },
      //           hidden: p.hidden == 1
      //         }
      //         route.children.push(son_route)
      //       }
      //     }
      //     routes.push(route)
      //   }
      // }
      // console.log(routes)
      // routes.forEach((v: any) => {
      //   router.addRoute(v)
      // })

      // console.log(arr[0])
      // let newRoute = {
      //   path: "/sys/config",
      //   component: Layout,
      //   name: "config",
      //   meta: {
      //     title: "基础配置"
      //   },
      //   children: [{
      //     path: "/sys/config/List",
      //     name: "configList",
      //     component: modules["../pages/sys/config/List.vue"],
      //   }]
      // }
      // console.log(newRoute)
      // router.addRoute(newRoute)
      next({ ...to, replace: true }) //重新刷新了一次
      // registerRouteFresh = false
      roleRoute.resetRoute()
    } else {
      next()
    }
  } else {
    // 没有登录
    if (to.path == '/login') {
      // 去登录页面直接放行
      next()
    } else {
      router.push('/login')
      ElMessage.error('请先完成登录')
      next()
    }
  }
})

router.afterEach(() => {
  close()
})

export default router
